import { defineStore } from "pinia";
import axios from "axios";
import { computed, ref } from "vue";

const useCinemaStore = defineStore("cinemas", () => {
  const cinemaList = ref([]);

  const getCinemaList = async (cityId) => {
    const res = await axios({
      url: `https://m.maizuo.com/gateway?cityId=${cityId}&ticketFlag=1&k=7546382`,
      headers: {
        "X-Client-Info":
          '{"a":"3000","ch":"1002","v":"5.2.1","e":"16927511235634589070458881","bc":"110100"}',
        "X-Host": "mall.film-ticket.cinema.list",
      },
    });

    cinemaList.value = res.data.data.cinemas;
  };

  const clearCinemaList = () => {
    cinemaList.value = [];
  };
  // const computedCinemaList = computed(() => {
  //   return (type) => {
  //     return cinemaList.value.filter((item) => item.eTicketFlag === type);
  //   };
  // });

  //简写
  const computedCinemaList = computed(() => (type) => {
    return cinemaList.value.filter((item) => item.eTicketFlag === type);
  });

  return {
    cinemaList,
    getCinemaList,
    clearCinemaList,
    computedCinemaList,
  };
});

export default useCinemaStore;
